package error_handler;

import java.text.*;
import java.util.*;

/**
 * Error handler
 */
public class ErrorHandler {
//    protected ResourceBundle resources[];
//    protected AbstractLogger logger;
//
//    /**
//     * Creates the error handler
//     */
//    public ErrorHandler(ResourceBundle bundle) {
//        this(bundle, null);
//    }
//
//    /**
//     * Creates the error handler
//     */
//    public ErrorHandler(ResourceBundle bundle, AbstractLogger logger) {
//        if (bundle == null)
//            throw new NullPointerException();
//        this.resources = new ResourceBundle[] { bundle };
//        this.logger = logger;
//    }
//
//    /**
//     * Gets the list of resource bundles
//     */
//    public synchronized ResourceBundle[] getResources() {
//        // Return a clone of the resources array
//        return (ResourceBundle[]) resources.clone();
//    }
//
//    /**
//     * Adds a resource bundle
//     */
//    public synchronized void addResources(ResourceBundle bundle) {
//        if (bundle == null)
//            throw new NullPointerException();
//        ResourceBundle temp[] = resources;
//        int length = resources.length;
//        resources = new ResourceBundle[length+1];
//        System.arraycopy(temp, 0, resources, 0, length);
//        resources[length] = bundle;
//    }
//
//    /**
//     * Gets the logger
//     */
//    public synchronized AbstractLogger getLogger() {
//        return logger;
//    }
//
//    /**
//     * Sets the logger
//     */
//    public synchronized void setLogger(AbstractLogger logger) {
//        this.logger = logger;
//    }

    /**
     * Logs a message and the exception
     */
    protected void log(String message, Exception exception) {
//        if (logger != null)
//            logger.log(message, exception);
//        else {
//            if (message != null)
//                System.err.println(message);
//            if (exception != null)
//                exception.printStackTrace();
//        }
    }

//* TRACE

    /**
     * Logs a trace
     */
    public synchronized void trace(String key) {
        String message = getErrorMessage(key, null);
        log(message, null);
    }

//* WARNING

    /**
     * Logs a warning
     */
    public synchronized void warning(String key) {
        warning(key, (String[]) null);
    }

    /**
     * Logs a warning
     */
    public synchronized void warning(String key, String arg0) {
        warning(key, new String[] { arg0 });
    }

    /**
     * Logs a warning
     */
    public synchronized void warning(String key, String arg0, String arg1) {
        warning(key, new String[] { arg0, arg1 });
    }

    /**
     * Logs a warning
     */
    public synchronized void warning(String key,
        String arg0, String arg1, String arg2) {
        warning(key, new String[] { arg0, arg1, arg2 });
    }

    /**
     * Logs a warning
     */
    public synchronized void warning(String key, String args[]) {
        String message = getErrorMessage(key, args);
        log(message, null);
    }

//* ERROR

    /**
     * Logs an error
     */
    public synchronized void error(String key) {
        error(key, null, (String[]) null);
    }

    /**
     * Logs an error
     */
    public synchronized void error(String key, Exception exception) {
        error(key, exception, (String[]) null);
    }

    /**
     * Logs an error
     */
    public synchronized void error(String key, Exception exception,
        String arg0) {
        error(key, exception, new String[] { arg0 });
    }

    /**
     * Logs an error
     */
    public synchronized void error(String key, Exception exception,
        String arg0, String arg1) {
        error(key, exception, new String[] { arg0, arg1 });
    }

    /**
     * Logs an error
     */
    public synchronized void error(String key, Exception exception,
        String arg0, String arg1, String arg2) {
        error(key, exception, new String[] { arg0, arg1, arg2 });
    }

    /**
     * Logs an error
     */
    public synchronized void error(String key, Exception exception,
        String args[]) {
        String message = getErrorMessage(key, args);
        log(message, exception);
    }

//* FATAL ERROR

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key)
        throws Exception {
        fatalError(key, null, (String[]) null);
    }

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key, Exception exception)
        throws Exception {
        fatalError(key, exception, (String[]) null);
    }

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key, Exception exception,
        String arg0) throws Exception {
        fatalError(key, exception, new String[] { arg0 });
    }

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key, Exception exception,
        String arg0, String arg1) throws Exception {
        fatalError(key, exception, new String[] { arg0, arg1 });
    }

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key, Exception exception,
        String arg0, String arg1, String arg2) throws Exception {
        fatalError(key, exception, new String[] { arg0, arg1, arg2 });
    }

    /**
     * Logs an error and throws the exception
     */
    public synchronized void fatalError(String key, Exception exception,
        String args[]) throws Exception {
        String message = getErrorMessage(key, args);
        log(message, exception);
        if (exception != null)
            throw exception;
        else
            throw new Exception(message);
    }

//* RESOURCE UTILS

    /**
     * Gets an error message
     */
    protected String getErrorMessage(String key, String args[]) {
        if (key == null)
            return "";
        String message = null;
//        for (int i = 0; i < resources.length; i++)
//            try {
//                message = resources[i].getObject(key).toString();
//                if (args == null)
//                    args = new String[0];
//                message = MessageFormat.format(message, args);
//            } catch (MissingResourceException e) {
//            }
        if (message == null) {
            StringBuffer buf = new StringBuffer();
            buf.append("MissingResource: ").append(key);
            if (args != null && args.length > 0) {
                buf.append(" (");
                for (int i = 0; i < args.length; i++)
                    buf.append(args[i]).append(", ");
                buf.setLength(buf.length()-2);
                buf.append(")");
            }
            message = buf.toString();
        }
        return message;
    }

}
